package com.fita.wechat.web.controller;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.result.HttpResult;
import com.fita.entity.dto.ehi.response.VehicleReservationCancleResponse;
import com.fita.entity.dto.ehi.response.VehicleReservationResponse;
import com.fita.entity.order.Order;
import com.fita.wechat.handler.EhiHandler;
import com.fita.wechat.service.OrderService;

/**
 * 
 * @author dan
 *
 */
@RestController
@RequestMapping("/test-vehicle")
public class VehicleTestController {
    
    @Autowired
    private OrderService orderService;
    
    @Autowired
    private EhiHandler ehiHandler;
    
    private Logger logger = LoggerFactory.getLogger(VehicleTestController.class);
    
    /**
     * 
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/reservation", method=RequestMethod.GET)
    public @ResponseBody HttpResult reservation(@RequestParam("order") Long orderId) {
        HttpResult httpResult = new HttpResult();
        try {
            Order order = orderService.load(orderId);
            Map<String, Object> params = orderService.generateReservation(order);
            VehicleReservationResponse response = ehiHandler.vehicleReservation(params);
            
            httpResult.success().data("response", response).message("下单成功");
        } catch (Exception e) {
            logger.error("reservation", e);
            httpResult.fail("500").message(e.getMessage());
        }
        return httpResult;
    }
    
    /**
     * 
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/cancel-reservation", method=RequestMethod.GET)
    public @ResponseBody HttpResult cancel(@RequestParam("order") Long orderId) {
        HttpResult httpResult = new HttpResult();
        try {
            Order order = orderService.load(orderId);
            Map<String, Object> params = orderService.generateCancel(order);
            VehicleReservationCancleResponse response = ehiHandler.vehicleReservationCancle(params);
            
            httpResult.success().data("response", response).message("取消成功");
        } catch (Exception e) {
            logger.error("cancel", e);
            httpResult.fail("500").message(e.getMessage());
        }
        return httpResult;
    }

}
